import java.util.HashMap;
import java.util.Stack;

/**
 * @program: LeetCode
 * @description: LeetCode : 496. 下一个更大元素 I
 * @author: WXY
 * @create: 2023-01-08 21:39
 * @Version 1.0
 **/
public class Num496_nextGreaterElement {
    public static int[] nextGreaterElement(int[] nums1, int[] nums2) {
        Stack<Integer> stack = new Stack<>();
        HashMap<Integer, Integer> map = new HashMap<>();
        stack.push(nums2[0]);
        for (int i = 1; i < nums2.length; i++) {
            while (!stack.isEmpty() && stack.peek() < nums2[i]) {
                map.put(stack.pop(), nums2[i]);
            }
            stack.push(nums2[i]);
        }
        int[] ans = new int[nums1.length];
        int index = 0;
        for (int num : nums1) {
            ans[index++] = map.getOrDefault(num, -1);
        }
        return ans;
    }
}
